package com.hontech.bread.device

import com.hontech.bread.R
import com.hontech.bread.ui.dialog.showErr
import com.hontech.bread.ui.dialog.showInfo
import com.hontech.bread.utils.ExecException
import com.hontech.bread.utils.hex16
import com.hontech.bread.utils.isBitSet
import com.hontech.bread.utils.log
import com.hontech.bread.utils.postEvent
import com.hontech.bread.utils.runOnUi
import com.hontech.bread.utils.stringById
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import serial.codec.ByteView
import serial.codec.Uint16
import serial.codec.Uint32
import serial.codec.Uint8


fun onNotify(frame: Frame) {

    try {
        dispatch(frame)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

private var menuFlag = false

private fun dispatch(frame: Frame) {
    val cmd = frame.cmd()

    when (cmd) {
        Cmd.STATUS -> {
            val status = DeviceStatus.withFrame(frame)
            postEvent(status)

            if (status.isResetClick()) {
                menuFlag = true
            } else {
                if (menuFlag) {
                    postEvent(MenuClickEvent())
                    menuFlag = false
                }
            }
        }

        Cmd.LOG -> {
            val bw = ByteView()
            frame.parse(bw)
            log("${stringById(R.string.t1733460848_203)}:$bw")
        }

        Cmd.BUTTON_CTRL_STATUS -> {
            val action = Uint8()
            val ec = Uint8()
            frame.parse(action, ec)
            val msg = when (action.value) {
                0 -> stringById(R.string.t1733460848_204)
                1 -> stringById(R.string.t1733460848_205)
                2 -> stringById(R.string.t1733460848_206)
                else -> stringById(R.string.t1733460848_207)
            }
            val err = errMsg(ec.value)
            runOnUi {
                if (ec.value != 0) {
                    showErr(msg, ExecException(err))
                } else {
                    showInfo(msg, err)
                }
            }
        }
    }
}

class ResetReplenishEvent
class MenuClickEvent


@Serializable
class DeviceStatus(
    val version: String,
    @SerialName("sw_sensor")
    val swSensor: Int,
    val adc: Int,
    val pt100: Int,
) {

    companion object {

        private var instance: DeviceStatus? = null

        fun get(): DeviceStatus {
            return instance ?: throw IllegalStateException(stringById(R.string.t1733460848_208))
        }

        fun withFrame(frame: Frame): DeviceStatus {
            val version = Uint16()
            val swSensor = Uint32()
            val adc = Uint16()
            val pt100 = Uint16()

            frame.parse(
                version,
                swSensor,
                adc,
                pt100
            )

            return DeviceStatus(
                version.value.hex16(),
                swSensor.value,
                adc.value,
                pt100.signedValue()
            )
        }
    }

    init {
        instance = this
    }


    fun isPt100Ok() = swSensor.isBitSet(30)
    fun isMenuClick() =
        swSensor.isBitSet(1) // ${stringById(R.string.t1733460848_209)}   ${stringById(R.string.t1733460848_210)}Reset${stringById(R.string.t1733460848_211)}

    fun isResetClick() = swSensor.isBitSet(18)

    fun isTempAllow() = (!swSensor.isBitSet(22)) && (!swSensor.isBitSet(23))
}
















